package com.aida.evil.eda.Window;

import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ProgressBar;

import com.aida.evil.aida.R;
import com.aida.evil.eda.Data.NetworkData;
import com.aida.evil.eda.Data.UserData;
import com.aida.evil.eda.MainActivity;
import com.aida.evil.eda.Toast.CustomSuperActivityToast;
import com.aida.evil.eda.Volley.VolleyHandler;
import com.aida.evil.eda.Volley.VolleyHttpRequest;
import com.github.johnpersano.supertoasts.SuperActivityToast;
import com.github.johnpersano.supertoasts.SuperToast;

import java.util.HashMap;
import java.util.Map;

/********************************************
 * 工程名：Aida
 * 包名：com.aida.evil.eda.Window
 * 时间：16-5-11 上午11:53
 * 作者：白雨濃 evil
 * 邮箱：evilbai@foxmail.com
 * 内容：主界面退出弹出的窗口，返回反馈信息，继承与MainActivity
 ********************************************/
public class MainActivity_ExitDialog extends MainActivity
{
    private static final String RETURNURL = "http://" + NetworkData.ServiceIP + "/ReturnInformation";

    private static EditText editText;
    private static LinearLayout linearLayout;
    private static ProgressBar progressBar;
    private static CustomDialog.Builder builder;

    public MainActivity_ExitDialog()
    {

    }

    /**
     * 显示窗口
     *
     * @param activity
     */
    public static void showDialog(final Activity activity)
    {
        builder = new CustomDialog.Builder(activity);
        builder.setTitle("退出");
        View view = View.inflate(activity, R.layout.activity_main_exitdialog, null);
        initView(view);/*初始化控件*/
        builder.setContentView(view);
        builder.setPositiveButton("退出", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                builder.dismiss();
                activity.finish();
            }
        });
        builder.setNeutralButton("提交", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                if (!editText.getText().toString().equals(""))
                {
                    linearLayout.setVisibility(View.GONE);
                    progressBar.setVisibility(View.VISIBLE);
                    toNetWork(activity);
                }
            }
        });
        builder.setNegativeButton("返回", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                builder.dismiss();
            }
        });
        builder.show();
    }

    /**
     * 初始化控件
     *
     * @param view
     */
    private static void initView(View view)
    {
        progressBar = (ProgressBar) view.findViewById(R.id.activity_mian_exitdialog_progressBar);
        editText = (EditText) view.findViewById(R.id.activity_mian_exitdialog_editText);
        linearLayout = (LinearLayout) view.findViewById(R.id.activity_mian_exitdialog_linearLayout);
    }

    /**
     * 上传反馈
     *
     * @param activity
     */
    private static void toNetWork(final Activity activity)
    {
        VolleyHandler<String> stringVolleyHandler = new VolleyHandler<String>()
        {
            @Override
            public void reqSuccess(String response)
            {
                builder.dismiss();
                activity.finish();
            }

            @Override
            public void reqError(String error)
            {
                progressBar.setVisibility(View.GONE);
                linearLayout.setVisibility(View.VISIBLE);
                Snackbar snackbar = builder.showToast("提交失败，请重试或退出！");
                snackbar.setAction("退出", new View.OnClickListener()
                {
                    @Override
                    public void onClick(View v)
                    {
                        builder.dismiss();
                        activity.finish();
                    }
                });
                snackbar.show();

            }
        };
        Map<String, String> map = new HashMap<>();
        map.put("userPhoneNum", UserData.DATA_PHONENUM);
        map.put("context", editText.getText().toString());
        VolleyHttpRequest.String_request(RETURNURL, map, stringVolleyHandler);

    }
}

